package com.jbp.job;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.jbp.model.Gift;
import com.jbp.model.GiftLot;
import com.jbp.model.PrizeDeploy;
import com.jbp.service.*;
import com.jbp.service.impl.LuckPoolRedisService;
import com.jbp.utils.LuckyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

//@Component
//@Configuration
public class CreatePrizePoolJob {

    @Autowired
    private PrizeDeployService prizeDeployService;

    @Autowired
    private GiftLotService giftLotService;

    @Autowired
    private LuckPoolRedisService luckPoolRedisService;
    @Autowired
    private GiftService giftService;

    @Autowired
    private LuckyUtil8 luckyUtil;
    @Autowired
    private PropertyService propertyService;

    @Scheduled(fixedDelay = 2000)
    public void createPool(){
        List<PrizeDeploy> list=prizeDeployService.list();
        list.forEach(li ->{
            /**
             * 查看剩余数量是否小于10000条
             */
            long count = luckPoolRedisService.count(li.getId());
            System.out.print("奖池");
            //奖池对应的礼物配置
            List<GiftLot> lots=new ArrayList<>();
            if (li.getState().equals("putong")){
                //普通奖池，直接根据奖池id获取
                 lots=giftLotService.list(Wrappers.<GiftLot>lambdaQuery()
                        .eq(GiftLot::getPoolId,li.getId())
                        .orderByAsc(GiftLot::getSort));
            }else{
                //特殊奖池 根据用户id和 奖池类型获取
                 lots=giftLotService.list(Wrappers.<GiftLot>lambdaQuery()
                        .eq(GiftLot::getState,li.getState())
                         .eq(GiftLot::getPoolType, li.getPoolType())
                        .orderByAsc(GiftLot::getSort));
            }

            if (lots != null && lots.size() > 0){
                int poolCount = lots.stream().mapToInt(lot -> lot.getGiftNum()).sum();
                if (count<poolCount && count<10000) {
                    System.out.println();
                    System.out.println("奖池生成");
                    //普通奖池
                    List<GiftLot> normalGiftLots = lots.stream().filter(giftLot -> "normal".equalsIgnoreCase(giftLot.getCategory()))
                            .collect(Collectors.toList());
                    normalGiftLots.forEach(giftLot -> {
                        Gift gift = giftService.getById(giftLot.getGiftId());
                        giftLot.setGift(gift);
                    });
                    if (normalGiftLots != null && normalGiftLots.size() > 0) {
                        LinkedList<Integer> linkedList = LuckyUtil.generate(normalGiftLots);
                        luckPoolRedisService.generate(linkedList, li.getId(), "normal");
                    }

                    //补池
                    /*List<GiftLot> auto1GiftLots = lots.stream().filter(giftLot -> "auto".equalsIgnoreCase(giftLot.getCategory()))
                            .collect(Collectors.toList());
                    auto1GiftLots.forEach(giftLot -> {
                        Gift gift = giftService.getById(giftLot.getGiftId());
                        giftLot.setGift(gift);
                    });
                    if (auto1GiftLots!=null && auto1GiftLots.size()>0) {
                        List<Integer> auto1List = LuckyUtil.generate(auto1GiftLots);
                        luckPoolRedisService.generate(auto1List, li.getId(), "auto");
                    }*/

                    //刷新礼物数量
                    luckPoolRedisService.addGiftCount(lots);

                    if (li.getUserId()==null) {
                        //每个礼物价格
                        Integer price = propertyService.getCoin(li.getPoolType());
                        //补池比率
                        double patchRate = Double.parseDouble(propertyService.getV("patchRate"));
                        //添加补池
                        luckyUtil.addPatch((int) (price * poolCount * patchRate));
                    }
                }
            }
        });
    }


}
